package com.obixlabs.security.credentialsstore.server.serviceimpl;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WebSSOSessionCleanUpThread implements Runnable
{	
	private static final Logger logger =
			LoggerFactory.getLogger(WebSSOSessionCleanUpThread.class);
		
	
	private WebSSOServiceImpl webSSOService;
	private long cleanupInterval;
	
	public WebSSOSessionCleanUpThread(
				WebSSOServiceImpl webSSOService,
				long cleanupInterval)
	{		
		setWebSSOService(webSSOService);
		setCleanupInterval(cleanupInterval);
		logger.info("Creating session cleanup thread.");
	}
		
	@Override
	public void run()
	{
		logger.info("Starting session cleanup thread.");
		try 
		{
			while (true)
			{
				Thread.sleep(cleanupInterval);
				logger.info("Running session cleanup cycle ...");
				
				Set<String> sessionIds = webSSOService.getIdsForAllSessions();
				logger.info("Found " +  sessionIds.size() 
							+ " saved and active sessions in total.");
				
				for (String sessionId : sessionIds)
					webSSOService.isSessionActive(sessionId);
			}						
		} 
		catch (InterruptedException intexce) {
			logger.warn("Cleanup thread has been interrupted. Will now exit!");
		}		
	}//end method def

	public WebSSOServiceImpl getWebSSOService(){return webSSOService;}
	private void setWebSSOService(WebSSOServiceImpl webSSOService)
	{this.webSSOService = webSSOService;}
	
	public long getCleanupInterval(){return cleanupInterval;}
	private void setCleanupInterval(long cleanupInterval)
	{this.cleanupInterval = cleanupInterval;}
}
